<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2018/1/2
 * Time: 10:02
 */
namespace app\super\controller;
use think\Db;
use app\super\validate\UserValidate;
class UserController extends CommonController {
    public function getUserInfo(){
        $where='';

        $rows = $_POST['rows'];
        $page = $_POST['page'];
        if($_POST['keyword']!=''){
            $keyword = trim($_POST['keyword']);
            $where['a.user_phone|a.user_username|a.real_name|a.user_post'] = array('like',"%$keyword%");
        }

        if($_POST['start']!=''){//时间
            $where['user_time'] =array('>=',$_POST['start']);
        }elseif ($_POST['end']!=''){
            $where['user_time'] =array('<=',$_POST['end']);
        }elseif ($_POST['start']!=''&&$_POST['end']!=''){
            $where['user_time'] =array('between',array($_POST['start'],$_POST['end']));
        }
//        if($_POST['end']!=''){//时间
//            $where['user_time'] =array('<=',$_POST['end']);
//        }
        if($_POST['province']!=''){
            $where['user_provice_id'] = $_POST['province'];
        }
        if($_POST['city']!=''){
            $where['user_city_id'] = $_POST['city'];
        }
        if($_POST['area']!=''){
            $where['user_u_area_id'] = $_POST['area'];
        }
        if($_POST['user_status']!=''){
            $where['user_status'] = $_POST['user_status'];
        }
        if($_POST['user_personal_status']!=''){
            $where['user_personal_status'] = $_POST['user_personal_status'];

        }
        $field = 'a.user_official,a.user_personal_status,udr.region_name,a.user_username,a.user_phone,a.user_post,a.real_name,a.user_area,a.user_register,a.user_status,a.user_time,a.user_id';
        $res = Db::table('up_u_user')->alias('a')
            ->join('up_dic_region udr','a.user_job=udr.region_id','left')
            ->where($where)
            ->field($field)
            ->limit($rows * ($page - 1), $rows)
            ->order('user_time DESC')
            ->select();
        $total = Db::table('up_u_user')->alias('a')
            ->join('up_dic_region udr','a.user_job=udr.region_id','left')
            ->where($where)
            ->limit($rows * ($page - 1), $rows)
            ->order('user_time DESC')
            ->count();
        $data['total'] = $total;
        $data['rows'] = $res;
        return json_encode($data);

    }
    /**
     * 查询省
     */
    public function province(){
        $province = Db::table('up_area')->where('level',1)->select();
        $data['province'] = $province;
        return json_encode($province);
    }

    /**
     * 查询市
     */
    public function city(){
        $city = $_POST['city'];
        $cityArray = Db::table('up_area')->where('pid',$city)->select();
        $data['cityArray'] = $cityArray;
        return json_encode($data);
    }
    /**
     * 查询区
     */
    public function area(){
        $area = $_POST['area'];
        $cityArray = Db::table('up_area')->where('pid',$area)->select();
        $data['cityArray'] = $cityArray;
        return json_encode($data);
    }

    public function addLife(){
        $region_id = 'P48DLGW9He9A63HFNyBqPnXxXTvdclqb';
        $province = Db::table('up_area')->where('level',1)->select();
        $user_job = Db::table('up_dic_region')->where('region_fid',$region_id)->select();
        $this->assign('province',$province);
        $this->assign('user_job',$user_job);
        return $this->fetch('user/addLife');
    }

    /**
     * @return string
     * 添加功能
     */
    public function insertLife(){
        $user_id = Db::table('up_u_user')->where('user_phone',$_POST['user_phone'])->find('user_id');
        if($user_id['user_id']){
            $returnData['status'] = 0;
            $returnData['msg'] = "该用户已经存在,请重新添加";
            return json_encode($returnData);
        }
        $validata = new UserValidate();
        if(!$validata->check($_POST)){
            $returnData['status'] = 0;
            $returnData['msg'] = $validata->getError();
            return json_encode($returnData);
        }else{
            if(preg_match("/^1[34578]{1}\d{9}$/",$_POST['user_phone'])){
            }else{
                $returnData['status'] = 0;
                $returnData['msg'] = "请输入有效的手机号码";
                return json_encode($returnData);
            }
            //身份证正面
            $rand = date('YmdHis').rand(100000,999999);
            $exe = explode('.',$_FILES['fileZ']['name']);
            $filePath = $_FILES['fileZ']['tmp_name'];
            $info = $this->qnCommon(2,$rand,$exe[1],$filePath);
            if($info['status']!=200) {
                $returnData['status'] = 0;
                $returnData['msg'] = "上传文件格式不正确";
                return json_encode($returnData);
            }else{
                $z_img = QiNiu.$info['key'];;
            }
            //身份证反面
            $rand = date('YmdHis').rand(100000,999999);
            $exe = explode('.',$_FILES['fileF']['name']);
            $filePath = $_FILES['fileF']['tmp_name'];
            $info = $this->qnCommon(2,$rand,$exe[1],$filePath);
            if($info['status']!=200) {
                $returnData['status'] = 0;
                $returnData['msg'] = "上传文件格式不正确";
                return json_encode($returnData);
            }else{
                $f_img = QiNiu.$info['key'];;
            }
            $result = $this->validation_filter_id_card($_POST['card_id']);
            if($result===false){
                $returnData['status'] = 0;
                $returnData['msg'] = "请输入有效的身份证号码";
                return json_encode($returnData);
            }
            $data['card_img'] = $z_img.','.$f_img;
            $data['user_id'] = $this->getRandChar(32);
            $data['user_phone'] = $_POST['user_phone'];
            $data['user_username'] = $_POST['user_username'];
            $data['real_name'] = $_POST['real_name'];
            $data['user_post'] = $_POST['user_post'];
            $data['user_birthday'] = $_POST['start'];
            $data['user_provice_id'] = $_POST['province'];
            $data['user_city_id'] = $_POST['city'];
            $data['user_u_area_id'] = $_POST['area'];
            $data['user_job'] = $_POST['user_job'];
            $data['user_signature'] = $_POST['user_signature'];
            $data['user_time'] = date('Y-m-d H:i:s');
            $data['user_register'] = 2;
            $data['card_id'] =$_POST['card_id'];
            $data['user_personal_status'] =2;
            $province = Db::table('up_area')->where('district_id',$_POST['province'])->value('district');//查询省
            $city = Db::table('up_area')->where('district_id',$_POST['city'])->value('district');//查询市
            $area = Db::table('up_area')->where('district_id',$_POST['area'])->value('district');//查询区
            $data['user_area'] = $province.'-'.$city.'-'.$area;
            $res = Db::table('up_u_user')->insert($data);
            if($res){
                return json_encode(array("status" => 1, "msg" => "发布成功！"));
            }else{
                return json_encode(array("status" => 0, "msg" => "发布失败！"));
            }

        }

    }
//停用
    public function updateLife(){
        $cid = $_POST['cid'];
        if($cid!=''){
            $where['user_id'] = $cid;
            $data['user_status'] = 2;//停用
            $res =  Db::table('up_u_user')->where($where)->update($data);
            if($res){
                $returnData['status'] = 1;
                $returnData['msg'] = "停用成功";
                return json_encode($returnData);
            }else{
                $returnData['status'] = 0;
                $returnData['msg'] = "停用失败";
                return json_encode($returnData);
            }
        }
    }
//启用
    public function refuseLife(){
        $cid = $_POST['cid'];
        if($cid!=''){
            $where['user_id'] = $cid;
            $data['user_status'] = 1;//启用
            $res =  Db::table('up_u_user')->where($where)->update($data);
            if($res){
                $returnData['status'] = 1;
                $returnData['msg'] = "启用成功";
                return json_encode($returnData);
            }else{
                $returnData['status'] = 0;
                $returnData['msg'] = "启用失败";
                return json_encode($returnData);
            }
        }
    }

    /**
     * 编辑页面
     */
    public function updateUser(){
        $nid = $_POST['nid'];
        $user = Db::table('up_u_user')->where('user_id',$nid)->find();
        $user['user_birthday'] = date('Y-d-m',strtotime($user['user_birthday']));
        $province = Db::table('up_area')->where('level',1)->select();
        $city = Db::table('up_area')->where('district_id',$user['user_city'])->select();
        $area = Db::table('up_area')->where('district_id',$user['user_u_area'])->select();
        $region_id = 'P48DLGW9He9A63HFNyBqPnXxXTvdclqb';
        $user_job = Db::table('up_dic_region')->where('region_fid',$region_id)->select();
        $this->assign('user_job',$user_job);
        $this->assign('user',$user);
        $this->assign('province',$province);
        $this->assign('city',$city);
        $this->assign('area',$area);
        return $this->fetch('user/updateUser');
    }

    /**
     * 编辑功能
     */
    public function updateUserInfo(){
        $where['user_phone'] = $_POST['user_phone'];
        $where['user_id'] = array('NEQ',$_POST['user_id']);
        $user_id = Db::table('up_u_user')->where($where)->find()['user_id'];
        if($user_id){
            $returnData['status'] = 0;
            $returnData['msg'] = "该用户已经存在,请重新添加";
            return json_encode($returnData);
        }
        $validata = new UserValidate();
        if(!$validata->check($_POST)){
            $returnData['status'] = 0;
            $returnData['msg'] = $validata->getError();
            return json_encode($returnData);
        }else{
            if(preg_match("/^1[34578]{1}\d{9}$/",$_POST['user_phone'])){
            }else{
                $returnData['status'] = 0;
                $returnData['msg'] = "请输入有效的手机号码";
                return json_encode($returnData);
            }
            $data['user_phone'] = $_POST['user_phone'];
            $data['user_username'] = $_POST['user_username'];
            $data['real_name'] = $_POST['real_name'];
            $data['user_post'] = $_POST['user_post'];
            $data['user_birthday'] = $_POST['start'];
            $data['user_provice'] = $_POST['province'];
            $data['user_city'] = $_POST['city'];
            $data['user_u_area'] = $_POST['area'];
            $data['user_job'] = $_POST['user_job'];
            $data['user_signature'] = $_POST['user_signature'];
            $province = Db::table('up_area')->where('district_id',$_POST['province'])->value('district');//查询省
            $city = Db::table('up_area')->where('district_id',$_POST['city'])->value('district');//查询市
            $area = Db::table('up_area')->where('district_id',$_POST['area'])->value('district');//查询区
            $data['user_area'] = $province.'-'.$city.'-'.$area;
            $res = Db::table('up_u_user') ->where('user_id',$_POST['user_id'])->update($data);
            if($res){
                return json_encode(array("status" => 1, "msg" => "修改成功！"));
            }else{
                return json_encode(array("status" => 0, "msg" => "修改失败！"));
            }

        }
    }

    /**
     * @return mixed
     * 认证页面
     */
    public function identification(){
        $res = Db::table('up_u_user')->where('user_id',$_POST['cid'])->field('user_id,card_id,card_img')->find();
        $img  = explode(',',$res['card_img']);
        $res['z_img'] = $img[0];
        $res['f_img'] = $img[1];
        $this->assign('res',$res);
        return $this->fetch('user/identification');
    }

    /**
     * 认证功能
     */
    public function updateIdentification(){
        $uid = $_POST['user_id'];
        if(!isset($_POST['user_personal_status'])){
            return json_encode(array("status" => 0, "msg" => "审核状态不能为空！"));

        }
        $where['user_id'] = $_POST['user_id'];
        $data['user_personal_status'] = $_POST['user_personal_status'];

        if($_POST['user_personal_status'] == 3){
            if($_POST['user_personal_reason']==''){
                return json_encode(array("status" => 0, "msg" => "审核认证不通过原因不能为空！"));
            }else{
                $data['user_personal_reason'] =$_POST['user_personal_reason'];
            }
        }
        $Res = Db::table('up_u_user')->where($where)->update($data);
        if($Res){
            if($_POST['user_personal_status'] == 2){
                $msg1 = "{\"msg\":\"2\"}";
                $code = 2;
                $code1 = 2;
                $msg='您提交的个人资质认证申请已通过,快去看看';
                $result = array('msg'=>$msg,'code'=>2,'fid'=>$uid);
                $this->sendJpush(1,$msg,$result,$uid,$msg1,$code,2,$code1);
            }else{
                $msg1 = "{\"msg\":\"3\"}";
                $code = 3;
                $code1 = 3;
                $msg='您提交的个人资质认证申请未通过,快去原因';
                $result = array('msg'=>$msg,'code'=>3,'fid'=>$uid);
                $this->sendJpush(1,$msg,$result,$uid,$msg1,$code,2,$code1);
            }


            return json_encode(array("status" => 1, "msg" => "审核通过！"));
        }else{

            return json_encode(array("status" => 0, "msg" => "审核不通过空！"));
        }

    }

    public function Setup(){
        $cid = $_POST['cid'];
        $user_official = Db::table('up_u_user')->where('user_id',$cid)->setField('user_official',1);
        if($user_official){
            return json_encode(array("status" => 1, "msg" => "修改成功！"));
        }else{
            return json_encode(array("status" => 0, "msg" => "修改失败！"));
        }
    }
    public function lower(){
        $cid = $_POST['cid'];
        $user_official = Db::table('up_u_user')->where('user_id',$cid)->setField('user_official',2);
        if($user_official){
            return json_encode(array("status" => 1, "msg" => "修改成功！"));
        }else{
            return json_encode(array("status" => 0, "msg" => "修改失败！"));
        }
    }
}